﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Management;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;

namespace Bear.Library.Util
{
    public class MachineCode
    {


        ///   <summary>    
        ///   获取cpu序列号        
        ///   </summary>    
        ///   <returns> string </returns>    
        public string GetCpuInfo()
        {
            string cpuInfo = " ";
            using (ManagementClass cimobject = new ManagementClass("Win32_Processor"))
            {
                ManagementObjectCollection moc = cimobject.GetInstances();

                foreach (ManagementObject mo in moc)
                {
                    cpuInfo = mo.Properties["ProcessorId"].Value.ToString();
                    mo.Dispose();
                }
            }
            return cpuInfo.ToString();
        }

        ///   <summary>    
        ///   获取硬盘ID        
        ///   </summary>    
        ///   <returns> string </returns>    
        public string GetHDid()
        {
            string HDid = " ";
            using (ManagementClass cimobject = new ManagementClass("Win32_DiskDrive"))
            {
                ManagementObjectCollection moc1 = cimobject.GetInstances();
                foreach (ManagementObject mo in moc1)
                {
                    HDid = (string)mo.Properties["Model"].Value;
                    mo.Dispose();
                }
            }
            return HDid.ToString();
        }

        ///   <summary>    
        ///   获取网卡硬件地址    
        ///   </summary>    
        ///   <returns> string </returns>    
        public string GetMoAddress()
        {
            string MoAddress = " ";
            using (ManagementClass mc = new ManagementClass("Win32_NetworkAdapterConfiguration"))
            {
                ManagementObjectCollection moc2 = mc.GetInstances();
                foreach (ManagementObject mo in moc2)
                {
                    if ((bool)mo["IPEnabled"] == true)
                        MoAddress = mo["MacAddress"].ToString();
                    mo.Dispose();
                }
            }
            return MoAddress.ToString();
        }


        public string Encode(string data, string key)
        {
            byte[] akey = ASCIIEncoding.ASCII.GetBytes(key);
            byte[] aIV = ASCIIEncoding.ASCII.GetBytes(key);
            using (DESCryptoServiceProvider CP = new DESCryptoServiceProvider())
            {
                MemoryStream ms = new MemoryStream();
                CryptoStream cs = new CryptoStream(ms, CP.CreateEncryptor(akey, aIV), CryptoStreamMode.Write);
                StreamWriter sw = new StreamWriter(cs);
                sw.Write(data);
                sw.Flush();
                cs.FlushFinalBlock();
                sw.Flush();
                return Convert.ToBase64String(ms.GetBuffer(), 0, (int)ms.Length);
            }
        }
        /// <summary>
        /// 加密算法（利用cpuid）
        /// </summary>
        /// <param name="data">需要解密的字符串</param>
        /// <returns></returns>
        public string Decode(string data, string key)
        {
            byte[] akey = ASCIIEncoding.ASCII.GetBytes(key);
            byte[] aIV = ASCIIEncoding.ASCII.GetBytes(key);
            byte[] Enc = null;
            try
            {
                Enc = Convert.FromBase64String(data);
            }
            catch
            {
                return null;
            }

            DESCryptoServiceProvider cp = new DESCryptoServiceProvider();
            MemoryStream ms = new MemoryStream(Enc);
            CryptoStream cs = new CryptoStream(ms, cp.CreateDecryptor(akey, aIV), CryptoStreamMode.Read);
            StreamReader reader = new StreamReader(cs);
            return reader.ReadToEnd();
        }
    }
}
